package com.hubu.web;

import com.hubu.mapper.administratorUserMapper;
import com.hubu.mapper.studentUserMapper;
import com.hubu.pojo.grade_Information;
import com.hubu.pojo.studentUser;
import com.hubu.pojo.student_Information;
import com.hubu.pojo.teacher_Information;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/studentHome")
public class StudentHomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            StringBuffer student=new StringBuffer("");
            Cookie[] cookies=request.getCookies();
            for(Cookie cookie : cookies)
            {
                String name=cookie.getName();
                if("username".equals(name)){
                     student.append(cookie.getValue());
                }
            }
            String studentId=new String(student);  //利用Cookie在找到登录的学生id

            System.out.println(studentId);
            if (studentId == null) {
                // 如果学号参数缺失或为空，处理错误或返回某种通知
                response.getWriter().println("Student ID is required.");
                return;
            }
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
                // 获取 Mapper
                studentUserMapper userMapper = sqlSession.getMapper(studentUserMapper.class);

                List<grade_Information> grades = userMapper.selectAllgrade(studentId);

                student_Information astudent = userMapper.selectStudent(studentId);

                // 将列表存储在请求属性中
                request.setAttribute("grades", grades);
                request.setAttribute("student", astudent);

                System.out.println(grades);

                // 转发到 adminHome.jsp 页面
                RequestDispatcher dispatcher = request.getRequestDispatcher("studentHome.jsp");
                dispatcher.forward(request, response);
            }
        } catch (Exception e) {
            // 在此处理异常，记录或适当地响应
            e.printStackTrace();  // 仅作演示之用，应替换为适当的日志记录或响应处理
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doGet(request,response);
    }

}